{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "accelerator": "GPU",
  "gpuClass": "standard"
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "view-in-github"
   },
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/d8ahazard/sd_dreambooth_extension/blob/Torch2/D8-DreamBooth.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "from google.colab import drive\n",
    "import os\n",
    "\n",
    "drive.mount(\"/content/gdrive\", force_remount=True)\n",
    "\n",
    "if not os.path.exists(\"/content/gdrive/MyDrive/sd-db-d8\"):\n",
    "  os.mkdirs(\"/content/gdrive/MyDrive/sd-db-d8\")\n",
    "\n",
    "if not os.path.exists(\"/content/working/\"):\n",
    "  os.mkdirs(\"/content/working/\")\n",
    "\n",
    "!rm -f /content/working/stable-diffusion-webui\n",
    "!ln -s /content/gdrive/MyDrive/sd-db-d8 /content/working/stable-diffusion-webui"
   ],
   "metadata": {
    "id": "W4eFr-nSHE85"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "Install various packages"
   ],
   "metadata": {
    "id": "PLbCegoEyO4M"
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "upF9RGj0tTiH"
   },
   "outputs": [],
   "source": [
    "# **fast-DreamBooth colab From https://github.com/d8ahazard/sd_dreambooth_extension, modified from https://github.com/camenduru/stable-diffusion-webui-collab**\n",
    "!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui /content/working/stable-diffusion-webui/\n",
    "!wget https://raw.githubusercontent.com/camenduru/stable-diffusion-webui-scripts/main/run_n_times.py -O /content/working/stable-diffusion-webui/scripts/run_n_times.py\n",
    "!git clone https://github.com/AlUlkesh/stable-diffusion-webui-images-browser /content/working/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser\n",
    "!git clone https://github.com/camenduru/stable-diffusion-webui-huggingface /content/working/stable-diffusion-webui/extensions/stable-diffusion-webui-huggingface\n",
    "!git clone https://github.com/camenduru/sd-civitai-browser /content/working/stable-diffusion-webui/extensions/sd-civitai-browser\n",
    "!git clone https://github.com/camenduru/sd-webui-additional-networks /content/working/stable-diffusion-webui/extensions/sd-webui-additional-networks\n",
    "!git clone -b Torch2 https://github.com/d8ahazard/sd_dreambooth_extension /content/working/stable-diffusion-webui/extensions/sd_dreambooth_extension\n",
    "\n",
    "%cd /content/working/stable-diffusion-webui\n"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "Bump Python Version (RUN ONCE)"
   ],
   "metadata": {
    "id": "no--xkmZyJGx"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "\n",
    "!wget https://github.com/korakot/kora/releases/download/v0.10/py310.sh\n",
    "!bash ./py310.sh -b -f -p /usr/local\n",
    "!python -m ipykernel install --name \"py310\" --user\n"
   ],
   "metadata": {
    "id": "IJxdOKeBvUGp"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "Download the 1.5 Model"
   ],
   "metadata": {
    "id": "sQirFY6cyr1O"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!wget https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned.safetensors -O /content/working/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned.safetensors\n"
   ],
   "metadata": {
    "id": "F42Hh4v8yjyB"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "And/Or download the 2.1 Model"
   ],
   "metadata": {
    "id": "K0uL_P3OyvC5"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!wget https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/v2-1_768-nonema-pruned.safetensors -O /content/working/stable-diffusion-webui/models/Stable-diffusion/v2-1_768-nonema-pruned.safetensors\n",
    "\n",
    "!wget https://huggingface.co/ckpt/stable-diffusion-2-1/raw/main/v2-inference-v.yaml -O /content/working/stable-diffusion-webui/models/Stable-diffusion/v2-1_768-nonemaema-pruned.yaml\n"
   ],
   "metadata": {
    "id": "FKyzSN7SyfbZ"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "Install Dreambooth and Auto1111 dependencies (RUN ONCE)"
   ],
   "metadata": {
    "id": "kzh_c4qvyFUs"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!%cd /content/working/stable-diffusion-webui/\n",
    "# Clone Auto1111 repos\n",
    "!git clone \"https://github.com/Stability-AI/stablediffusion.git\" \"/content/working/stable-diffusion-webui/repositories/stable-diffusion-stability-ai\" \n",
    "!%cd \"/content/working/stable-diffusion-webui/repositories/stable-diffusion-stability-ai\" \n",
    "!git checkout \"47b6b607fdd31875c9279cd2f4f16b92e4ea958e\"\n",
    "!git clone \"https://github.com/CompVis/taming-transformers.git\" \"/content/working/stable-diffusion-webui/repositories/taming-transformers\" \n",
    "!%cd \"/content/working/stable-diffusion-webui/repositories/taming-transformers\" \n",
    "!git checkout \"24268930bf1dce879235a7fddd0b2355b84d7ea6\"\n",
    "!git clone \"https://github.com/crowsonkb/k-diffusion.git\" \"/content/working/stable-diffusion-webui/repositories/k-diffusion\"\n",
    "!%cd \"/content/working/stable-diffusion-webui/repositories/k-diffusion\"\n",
    "!git checkout \"5b3af030dd83e0297272d861c19477735d0317ec\"\n",
    "!git clone \"https://github.com/sczhou/CodeFormer.git\" \"/content/working/stable-diffusion-webui/repositories/CodeFormer\"\n",
    "!%cd \"/content/working/stable-diffusion-webui/repositories/CodeFormer\" \n",
    "!git checkout \"c5b4593074ba6214284d6acd5f1719b6c5d739af\"\n",
    "!git clone \"https://github.com/salesforce/BLIP.git\" \"/content/working/stable-diffusion-webui/repositories/BLIP\" \n",
    "!%cd \"/content/working/stable-diffusion-webui/repositories/BLIP\"\n",
    "!git checkout \"48211a1594f1321b00f14c9f7a5b4813144b2fb9\"\n"
   ],
   "metadata": {
    "id": "q4-o5-Grx_sw"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "Setup VENV"
   ],
   "metadata": {
    "id": "_G-NX4mvIv-G"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Install PIP stuff\n",
    "!%cd /content/working/stable-diffusion-webui/\n",
    "!pip install virtualenv\n",
    "!virtualenv venv\n",
    "!source venv/bin/activate\n",
    "# Install Auto1111 PIP packages\n",
    "!pip install git+https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379\n",
    "!pip install git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1\n",
    "!pip install git+https://github.com/mlfoundations/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b\n",
    "\n",
    "# Install Requirements\n",
    "!pip install -r /content/working/stable-diffusion-webui/requirements_versions.txt\n",
    "!pip install -r /content/working/stable-diffusion-webui./extensions/sd_dreambooth_extension/requirements.txt\n",
    "!pip install https://github.com/ArrowM/xformers/releases/download/xformers-0.0.17%2B36e23c5.d20230209-cp310-cu118/xformers-0.0.17+36e23c5.d20230209-cp310-cp310-linux_x86_64.whl\n",
    "!pip install https://download.pytorch.org/whl/nightly/cu118/torch-2.0.0.dev20230209%2Bcu118-cp310-cp310-linux_x86_64.whl https://download.pytorch.org/whl/nightly/cu118/torchvision-0.15.0.dev20230209%2Bcu118-cp310-cp310-linux_x86_64.whl\n"
   ],
   "metadata": {
    "id": "XIdP81K-Isiw"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "Run Auto1111"
   ],
   "metadata": {
    "id": "A_AePD55yYeQ"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "!%cd /content/working/stable-diffusion-webui/extensions/sd_dreambooth_extension/\n",
    "!git fetch && git pull\n",
    "!%cd /content/working/stable-diffusion-webui/ \n",
    "!python launch.py --share --xformers --enable-insecure-extension-access --torch2 --skip-install --skip-torch-cuda-test"
   ],
   "metadata": {
    "id": "qa5T38izv6CX"
   },
   "execution_count": null,
   "outputs": []
  }
 ]
}
